package com.ruoyi.business.controller;

import com.alibaba.fastjson.JSON;
import com.ruoyi.auth.form.Code;
import com.ruoyi.business.service.IBusinessLoginService;
import com.ruoyi.business.uitls.SmsUtil;
import com.ruoyi.common.core.constant.CacheConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Random;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/business")
public class BusinessLoginController {
    @Autowired
    private RedisService redisService;



    @Autowired
    private IBusinessLoginService businessLoginService;



    @PostMapping("/login/{phone}/{code}")
    public R<?> login(@PathVariable("phone") String phone,@PathVariable("code")String code) {


        /*// 用户登录 方法
        LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword());
        // 获取登录token
        return R.ok(tokenService.createToken(userInfo));*/
        return businessLoginService.login(phone, code);
    }

    @RequestMapping ("/createCode")
    public R<?> creatCode(@RequestParam("phone") String phone) {
        System.out.println(phone);
        //生成四位随机数
        int code = (new Random().nextInt(9000)+999);
        System.out.println("code："+code);
        //将随机数放入Redis
        redisService.setCacheObject(CacheConstants.CAPTCHA_CODE_KEY+phone, code, 1L, TimeUnit.MINUTES);
        //发送短信验证码
        SmsUtil.sendSms(phone, "阿里云短信测试", "SMS_154950909", JSON.toJSONString(new Code(code)));
        System.out.println("发送成功");
        return R.ok();
    }
}